package com.kygeng.springframework.common;

import com.kygeng.springframework.bean.Address;
import com.kygeng.springframework.bean.UserInfo;
import com.kygeng.springframework.beans.BeansException;
import com.kygeng.springframework.beans.factory.config.BeanPostProcessor;

public class UserInfoBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("UserInfoBeanPostProcessor # postProcessBeforeInitialization");
        //从上海搬家到北京
        if (bean instanceof UserInfo) {
            UserInfo userInfo = (UserInfo) bean;
            userInfo.setAddress(new Address("北京省", "北京市"));
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("UserInfoBeanPostProcessor # postProcessAfterInitialization");
        if (bean instanceof UserInfo) {
            UserInfo userInfo = (UserInfo) bean;
            userInfo.setAge(20);
        }
        return bean;
    }
}